package ui.magic.magiclibs.container.View;

import android.content.Context;
import android.graphics.Point;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import java.util.Vector;

import ui.magic.magiclibs.R;
import ui.magic.magiclibs.container.Frame.FrameMagicUi;
import ui.magic.magiclibs.container.View.Observer.ChangeItemObserver;

/**
 * Created by liushiyin on 2017/10/25.
 */

public class AdvertisePlayer extends FrameMagicUi implements ChangeItemObserver,View.OnTouchListener{

    private Vector<Integer> Resids;
    private int curAdvertiseBgIndex;
    private ImageView advertiseBg;
    private ChangeItemBar changeItemBar;
    private Point start;

    public AdvertisePlayer(@NonNull Context context) {
        super(context);
        init();
    }

    public AdvertisePlayer(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdvertisePlayer(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        Resids=new Vector<>();
        start=new Point();
        Resids.add(R.drawable.bg21);
        changeItemBar=new ChangeItemBar(getContext());
        advertiseBg=new ImageView(getContext());
        curAdvertiseBgIndex=0;
        advertiseBg.setImageResource(Resids.get(curAdvertiseBgIndex));
        advertiseBg.setScaleType(ImageView.ScaleType.FIT_XY);
        this.addChild(advertiseBg,100,100,FrameMagicUi.CENTER);
        this.addChild(changeItemBar,100,8,FrameMagicUi.BOTTOM);
        changeItemBar.setObserver(this);
        advertiseBg.setOnTouchListener(this);
    }

    public void setAdvertiseNums(int nums){
        changeItemBar.setItems(nums);
    }

    public void addAdvertiseRes(int resid){
        Resids.add(resid);
    }

    public void setUnderItemColor(int color){
        changeItemBar.setItemColor(color);
    }

    @Override
    public void onChange(int type) {
        if(type>0){
            curAdvertiseBgIndex=(curAdvertiseBgIndex+1)%Resids.size();
            advertiseBg.setImageResource(Resids.get(curAdvertiseBgIndex));
        }else{
            curAdvertiseBgIndex=(curAdvertiseBgIndex-1)<0?Resids.size()-1:curAdvertiseBgIndex-1;
            advertiseBg.setImageResource(Resids.get(curAdvertiseBgIndex));
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                start.x=(int) event.getX();
                break;
            case MotionEvent.ACTION_UP:
                if(start.x-(int) event.getX()<0){
                    changeItemBar.changeLastItem();
                }else if(start.x-(int) event.getX()>0){
                    changeItemBar.changeNextItem();
                }
                break;
        }
        return true;
    }
}
